package com.micro.controller;

import com.micro.dto.OrderResultDTO;
import com.micro.request.TicketCreatingRequest;
import com.micro.request.MealOrderRequest;
import com.micro.request.HotelOrderRequest;
import com.micro.service.UnifiedOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 统一订单控制器
 * 提供完整的购买流程接口
 */
@RestController
@RequestMapping("/api/orders")
public class UnifiedOrderController {

    private static final Logger logger = LoggerFactory.getLogger(UnifiedOrderController.class);

    @Autowired
    private UnifiedOrderService unifiedOrderService;

    /**
     * 创建火车票订单（完整流程）
     */
    @PostMapping("/train-ticket")
    public ResponseEntity<OrderResultDTO> createTrainTicketOrder(@RequestBody TicketCreatingRequest request) {
        try {
            logger.info("收到火车票订单创建请求，用户: {}, 车次: {}", request.getUserName(), request.getTrainNumber());
            
            OrderResultDTO result = unifiedOrderService.createTrainTicketOrder(request);
            
            if (result.isSuccess()) {
                logger.info("火车票订单创建成功，订单ID: {}", result.getOrderId());
                return ResponseEntity.ok(result);
            } else {
                logger.warn("火车票订单创建失败: {}", result.getMessage());
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            logger.error("创建火车票订单异常", e);
            OrderResultDTO errorResult = new OrderResultDTO(false, "系统异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResult);
        }
    }

    /**
     * 批量创建火车票订单（完整流程）
     */
    @PostMapping("/train-ticket/batch")
    public ResponseEntity<OrderResultDTO> createTrainTicketOrders(@RequestBody List<TicketCreatingRequest> requests) {
        try {
            logger.info("收到批量火车票订单创建请求，订单数量: {}", requests.size());
            
            OrderResultDTO result = unifiedOrderService.createTrainTicketOrders(requests);
            
            if (result.isSuccess()) {
                logger.info("批量火车票订单创建成功");
                return ResponseEntity.ok(result);
            } else {
                logger.warn("批量火车票订单创建失败: {}", result.getMessage());
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            logger.error("批量创建火车票订单异常", e);
            OrderResultDTO errorResult = new OrderResultDTO(false, "系统异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResult);
        }
    }

    /**
     * 创建火车餐订单（完整流程）
     */
    @PostMapping("/train-meal")
    public ResponseEntity<OrderResultDTO> createTrainMealOrder(@RequestBody MealOrderRequest request) {
        try {
            logger.info("收到火车餐订单创建请求，用户: {}", request.getUsername());
            
            OrderResultDTO result = unifiedOrderService.createTrainMealOrder(request);
            
            if (result.isSuccess()) {
                logger.info("火车餐订单创建成功，订单ID: {}", result.getOrderId());
                return ResponseEntity.ok(result);
            } else {
                logger.warn("火车餐订单创建失败: {}", result.getMessage());
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            logger.error("创建火车餐订单异常", e);
            OrderResultDTO errorResult = new OrderResultDTO(false, "系统异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResult);
        }
    }

    /**
     * 创建酒店订单（完整流程）
     */
    @PostMapping("/hotel")
    public ResponseEntity<OrderResultDTO> createHotelOrder(@RequestBody HotelOrderRequest request) {
        try {
            logger.info("收到酒店订单创建请求，用户: {}, 酒店: {}", request.getUserName(), request.getHotelName());
            
            OrderResultDTO result = unifiedOrderService.createHotelOrder(request);
            
            if (result.isSuccess()) {
                logger.info("酒店订单创建成功，订单ID: {}", result.getOrderId());
                return ResponseEntity.ok(result);
            } else {
                logger.warn("酒店订单创建失败: {}", result.getMessage());
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            logger.error("创建酒店订单异常", e);
            OrderResultDTO errorResult = new OrderResultDTO(false, "系统异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResult);
        }
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("统一订单服务运行正常");
    }
}
